package com.grapevine.contact.adapter;

import com.grapevine.contact.R;
import android.content.Context;
import android.database.Cursor;
import android.provider.CallLog;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;

/**
 * 读取数据后，将数据库填充到指定好的行中
 * 
 * @author Snake
 * 
 */

public class RecentAdapter extends ResourceCursorAdapter
{
	final int DAY = 1440; // 一天的分钟值
	private int[] mTo;
	private String[] mOriginalFrom;
	private Context mContext;

	public RecentAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
	{
		super(context, layout, c);
		mContext = context;
		mOriginalFrom = from;
		mTo = to;
	}


	@Override
	public void bindView(View view, Context context, Cursor cursor)
	{
		// TODO Auto-generated method stub
		ImageView mailButton = (ImageView) view.findViewById(R.id.MailButton);
		View[] views = new View[mTo.length];
		for (int i = 0; i < views.length; i++)
		{
			views[i] = view.findViewById(mTo[i]);
			String value = cursor.getString(cursor.getColumnIndex(mOriginalFrom[i]));

			if (views[i] instanceof TextView)
			{
				if ("date".equals(mOriginalFrom[i]))
				{
					long callTime = Long.parseLong(value);
					// long newTime = new Date().getTime();
					// long duration = (newTime - callTime) / (1000 * 60);

					CharSequence content = DateUtils.getRelativeTimeSpanString(callTime, System.currentTimeMillis(),
							DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_NUMERIC_DATE);
					// if (duration < 60)
					// {
					// value = duration + "分钟前";
					//						
					// }
					// else if (duration >= 60 && duration < DAY)
					// {
					// value = (duration / 60) + "小时前";
					// }
					// else if (duration >= DAY && duration < DAY * 2)
					// {
					// value = "昨天";
					// }
					// else if (duration >= DAY * 2 && duration < DAY * 3)
					// {
					// value = "前天";
					// }
					// else if (duration >= DAY * 7)
					// {
					// SimpleDateFormat sdf = new SimpleDateFormat("M月dd日");
					// value = sdf.format(new Date(callTime));
					// }
					// else
					// {
					// value = (duration / DAY) + "天前";
					// }

					setText((TextView) views[i], content.toString());
					continue;
				}

				else if ("name".equals(mOriginalFrom[i]))
				{
					if (null == value || "".equals(value))
					{
						value = cursor.getString(cursor.getColumnIndex("number"));
					}
				}

				setText((TextView) views[i], value);
			}
			else if ("type".equals(mOriginalFrom[i]))
			{
				int type = Integer.parseInt(value);
				if (CallLog.Calls.INCOMING_TYPE == type)
				{
					mailButton.setBackgroundResource(R.drawable.ic_call_log_list_incoming_call); // 更改行颜色
				}
				else if (CallLog.Calls.OUTGOING_TYPE == type)
				{
					mailButton.setBackgroundResource(R.drawable.ic_call_log_list_outgoing_call);
				}
				else if (CallLog.Calls.MISSED_TYPE == type)
				{
					mailButton.setBackgroundResource(R.drawable.ic_call_log_list_missed_call);
				}
			}
		}
	}

	public void setText(TextView v, String text)
	{
		v.setText(text);
	}
}